<template>
  <div class="buttonAdd">
    <el-dialog :close-on-click-modal="false" :append-to-body="true" @open="dialogOpen"  title="总量编辑" :visible.sync="dialogFormVisibleAdd" @opened="$store.commit('dialogTop')" @close="resetForm('ruleFormAdd')">
      <el-form :model="ruleFormAdd" :rules="rulesAdd" ref="ruleFormAdd" label-width="80px" class="demo-ruleForm">
        <el-form-item label="需求总量" prop="volume" :rules="{required: true, message: '需求总量不能为空', 'trigger': 'blur'  }">
          <el-input  size="small" v-model="ruleFormAdd.volume" auto-complete="off" placeholder="需求总量"></el-input>
        </el-form-item>
        <el-form-item label="预警阀值" prop="alarmValue" :rules="{required: true, message: '预警阀值不能为空','trigger': 'blur'  }">
          <el-input  size="small"  :maxlength="20" v-model="ruleFormAdd.alarmValue" auto-complete="off" placeholder="预警阀值"></el-input>
        </el-form-item>
      </el-form>
      <div style="text-align: center;margin-top: 21px;">
        <el-button  size="small" @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button style="margin-right: 20px;" type="primary" size="small" @click="submitForm('ruleFormAdd')">确 定</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>

  export default {
    props:["editInfo"],
    data () {
      var phoneCheck = (rule, value, callback) => {
        var phnum = /^1[3456789]{1}\d{9}$/
        if (value === '') {
          callback(new Error('联系电话不能为空'));
        }else if(!phnum.exec(Number(value))){
          callback(new Error('请正确输入手机号！'));
        } else {
          callback();
        }
      };
      return {
        ruleFormAdd: {
          id:"",
          "alarmValue":"",
          "volume":""
        },
        dialogFormVisibleAdd: false,
        rulesAdd: {
          mobile: [
            {validator: phoneCheck, }
          ],
        }
      }
    },
    created(){

    },
    methods: {
      dialogOpen(){
        this.ruleFormAdd.id=this.editInfo.id
        this.ruleFormAdd.alarmValue=this.editInfo.alarmValue
        this.ruleFormAdd.volume=this.editInfo.volume
      },
      //提交回调
      handleDiaSureAdd(){
        this.axios.post(process.env.API_HOST + '/put/updateRegionPut', this.ruleFormAdd)
          .then(response => {
            let result=response.data
            if (result.code == 200) {
              this.$message({
                type: 'success',
                message: '修改成功!'
              });
              this.dialogFormVisibleAdd = false
              this.$emit('searchRowsInfo')
            }
          })
          .catch(error => {
            console.log(error);
          });
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.handleDiaSureAdd()
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus" >
  @import "../../../common/stylus/mixin.styl"
  .buttonAdd
    display inline-block
    margin-left 20px
</style>
